<!DOCTYPE html>
<script src="/js-test-resources/js-test.js"></script>
<script src="resources/get-request-header.js"></script>
<script src="resources/cookie-test-util.js"></script>
<script>
description('Test receiving cookies in a WebSocket handshake which were set by another WebSocket handshake.');

window.jsTestIsAsync = true;

var cookie;
var URL_SET_COOKIE = 'ws://127.0.0.1:8880/set-cookie';
var ws_set_cookie = new WebSocket(URL_SET_COOKIE);

ws_set_cookie.onopen = function () {
    ws_set_cookie.close();
};
ws_set_cookie.onclose = function (e) {
    connectAndGetRequestHeader('cookie').then(function(value) {
        cookie = normalizeCookie(value);
        shouldBeEqualToString('cookie', 'same-site-lax=1; same-site-strict=1; ws-domain-local-ip=1; ws-path-root=1; ws=1');
        clear();
    }, finishAsFailed);
};

function clear() {
    var ws = new WebSocket(URL_SET_COOKIE + '?clear=1');
    ws.onopen = function () {
        ws.close();
    };
    ws.onclose = finishJSTest;
}
setTimeout(finishJSTest, 1000);
</script>
